package com.yycx.module.bus.provider.handler.haiying.order;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.GetInterceptor;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.BusCustom;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.client.entity.hy.BusBankStatement;
import com.yycx.module.bus.client.entity.hy.BusHyorderSendinfo;
import com.yycx.module.bus.client.entity.hy.BusPaymentOrder;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.provider.service.BusCustomService;
import com.yycx.module.bus.provider.service.BusPaymentService;
import com.yycx.module.bus.provider.service.BusSalemanService;
import com.yycx.module.bus.provider.service.hy.BusBankStatementService;
import com.yycx.module.bus.provider.service.hy.BusHyorderSendinfoService;
import com.yycx.module.bus.provider.service.hy.BusPaymentOrderService;
import com.yycx.module.bus.provider.service.hy.HyOrderService;
import com.yycx.module.system.client.entity.SysBank;
import com.yycx.module.system.provider.service.SysBankService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 海鹰订单详情信息
 *
 * @author zyf
 */
@Component
public class HyInvoiceGetHandler implements GetInterceptor {

    @Autowired
    private HyOrderService orderService;


    @Autowired
    private BusHyorderSendinfoService hyorderSendinfoService;


    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery cq, EntityMap entityMap) {
    }

    @Override
    public void complete(CriteriaQuery cq, EntityMap map) {
        String orderId = map.get("orderId");
        Long saleManId = map.getLong("saleManId");
        Long customId = map.getLong("customId");
        HyOrder lastOrder = orderService.selectLastOrderByCustomId(saleManId, customId);
        if (FlymeUtils.isNotEmpty(lastOrder)) {
            BusHyorderSendinfo lastSendInfo = hyorderSendinfoService.findByOrderId(lastOrder.getOrderId());
            if (FlymeUtils.isNotEmpty(lastSendInfo)) {
                map.put("invoiceTel", lastSendInfo.getInvoiceTel());
                map.put("invoiceAddress", lastSendInfo.getInvoiceAddress());
                map.put("customFpId", lastSendInfo.getCustomFpId());
            }
        }
        //开票信息
        BusHyorderSendinfo sendInfo = hyorderSendinfoService.findByOrderId(orderId);
        if (FlymeUtils.isNotEmpty(sendInfo)) {
            map.put("invoiceNo", sendInfo.getInvoiceNo());
            map.put("sendInfoId", sendInfo.getSendInfoId());
            map.put("fastMailNo", sendInfo.getFastMailNo());
            map.put("fastMailName", sendInfo.getFastMailName());
            map.put("updateFastMailNo", sendInfo.getUpdateFastMailNo());
            map.put("updateFastMailName", sendInfo.getUpdateFastMailName());
            map.put("invoiceDate", DateUtils.formatDate(sendInfo.getInvoiceDate()));
            map.put("invoiceTel", sendInfo.getInvoiceTel());
            map.put("invoiceAddress", sendInfo.getInvoiceAddress());
            map.put("customFpId", sendInfo.getCustomFpId());
        }
    }
}
